﻿using MediatR;
using Microsoft.VisualBasic;
using Traceability.App.Api.Application.Command.Basic;
using Traceability.App.ErrorCount;
using Traceability.App.InfrasIntructure;
using Traceability.App.Model;

namespace Traceability.App.Api.Application.Handler.Basic
{
    public class DelMultipleBasicHandler : IRequestHandler<DelMultipleBasicCommand, ApiResult<int>>
    {
        IBaseRepository<BasicModel> basicRepo;

        public DelMultipleBasicHandler(IBaseRepository<BasicModel> basicRepo)
        {
            this.basicRepo = basicRepo;
        }

        public async Task<ApiResult<int>> Handle(DelMultipleBasicCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> res=new ApiResult<int>();
            var Deletes=basicRepo.GetAll().Where(c=>request.Ids.Contains(c.Id)).ToList();
            if(Deletes==null)
            {
                res.Msg = "找不到编号";
                res.Code=ApiEnum.Fail;
                return await  Task.FromResult(res);
            }
            foreach (var Delete in Deletes)
            {
                Delete.IsDeleted = true;

            }
            var updateDel = await basicRepo.UpdateAll(Deletes);
            res.Datas = updateDel;
            res.Code = updateDel > 0 ? ApiEnum.Success : ApiEnum.Fail;
            res.Msg = updateDel > 0 ? "批量删除成功" : "批量删除失败";
            return await Task.FromResult(res);
        }
    }
}
